package cn.xorange.commons.utils.lang;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.util.Date;

import static cn.hutool.core.date.DatePattern.NORM_DATETIME_PATTERN;
import static cn.hutool.core.date.DatePattern.NORM_DATE_PATTERN;

/**
 * @author : yangjian
 * @date : 2024/1/
 */
public class DateUtils {

    private DateUtils(){}

    public static String now(){
        return DatePattern.NORM_DATETIME_FORMAT.format(new DateTime());
    }

    public static Date dayStart(Date date){
        String sDate = DateUtil.format(date,NORM_DATE_PATTERN) + " 0:00:00";
        return DateUtil.parse(sDate,NORM_DATETIME_PATTERN);
    }

    public static Date dayEnd(Date date){
        String eDate = DateUtil.format(date,NORM_DATE_PATTERN) + " 23:59:59";
        return DateUtil.parse(eDate,NORM_DATETIME_PATTERN);
    }

    /**
     * 计算时间差
     *
     * @param endDate 最后时间
     * @param startTime 开始时间
     * @return 时间差（天/小时/分钟）
     */
    public static String timeDistance(Date endDate, Date startTime)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startTime.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }
}
